
from com.utils.DbHelper import DbHelper

class UserController():
    def register(self,username,password):
        # 校验用户名是否为空
        if username == None or password == None or username.strip() == "" or password.strip() == "":
            return {"status":400,"msg":"用户名密码不能为空"}
        else:
            db = DbHelper("test")
            sql1 = "select * from user where username = %s"
            user = db.queryOne(sql1,(username,))
            if user is None:
        # 获取连接对象，游标对象
        # conn = pymysql.connections.Connection(host="127.0.0.1",user="root",password="root",port=3306,db="test",charset="utf8")
        # cursor = conn.cursor()
                dbhelper = DbHelper("test")
                sql = "insert into user(username,password) values(%s,%s)"
                n = dbhelper.update(sql,(username,password))
        # sql = "insert into user(username,password) values('{}','{}')".format(username,password)
        # n = cursor.execute(sql)
        # conn.commit()
        # cursor.close()
        # conn.close()
                if n > 0:
                    return {"status":200,"msg":"注册成功"}
                else:
                    return {"status":500,"msg":"注册异常"}
            else:
                return {"status":300,"msg":"用户名已被注册"}
    def login(self,username,password):
        sql = "select * from user where username=%s and password=%s"
        dbhelper = DbHelper("test")
        result = dbhelper.queryOne(sql,(username,password))
        if(result is not None):
            return {"status":200,"msg":"登录成功"}
        else:
            return {"status":400,"msg":"用户不存在"}
if __name__ == '__main__':
    user = UserController()
    result = user.login("admin","admin")
    assert result["status"] == 200

